<?php
class SeeFileController extends AppController{
    
    /**
     * 
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->menu_active = 'see_file';
        
    }
    
    /**
     * 
     */
    public function index(){
        $this->loadModel('Record');
        App::import('vendor','pagination',array('file' => 'Pagination/Pagination.class.php'));
        $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
        $limit = 10;
        $offset = $page?($page -1) * $limit:0;
        $pagination = (new Pagination());
        $pagination->setCurrent($page);
        $pagination->setTotal($this->Record->getRecordConditions($this->request->data,$offset,$limit,true));
        $markup = $pagination->parse();
        $this->set('markup', $markup);
        $cameras = $this->Record->getRecordConditions($this->request->data,$offset,$limit);
        $this->set('cameras', $cameras);
    }
    
    /**
     * 
     */
    public function view($id = null){
        $this->layout = false;
        if (!$id) {
            throw new NotFoundException(__('Invalid Record'));
        }
        $this->loadModel('Record');
        //$record = $this->Record->find('first',array('conditions' => array('Record.Id' => $id)));
        $record = $this->Record->getFileById($id);
        if (!$record) {
            throw new NotFoundException(__('Invalid Record'));
        }
        $this->set('record',$record);
    }
    
    /**
     * 
     */
    public function ajax($id = 0){
        $this->layout = "ajax_file";
        $condition['Camera']['Id'] = $id;
        $this->loadModel('Record');
        /**
        App::import('vendor','pagination',array('file' => 'Pagination/Pagination.class.php'));
        $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
        $limit = 10;
        $offset = $page?($page -1) * $limit:0;
        $pagination = (new Pagination());
        $pagination->setCurrent($page);
        $pagination->setTotal($this->Record->getRecordConditions($condition,$offset,$limit,true));
        $markup = $pagination->parse();
        $this->set('markup', $markup);
        $cameras = $this->Record->getRecordConditions($condition,$offset,$limit);
        
         */
        $cameras = $this->Record->getRecordConditions($condition,0,10,false);
        $this->set('cameras', $cameras);
        $this->set('markup', "");
        $this->set('camera_id',$id);
    }
    
    /**
     * 
     */
    public function delete($id = 0,$ajax = 0,$camera_id = 0){
        $this->loadModel('Record');
        $record = $this->Record->find('first',array('conditions' => array('Record.Id' => $id)));
        if (!$record) {
            throw new NotFoundException(__('Invalid record'));
        }
        $file = PATH_ROOT.'video/'.$record['Record']['RecPath'];
        if(file_exists($file)){
            unlink($file);
        }
        $this->Record->delete($id);
        if(!$ajax){
            return $this->redirect(array('controller' => 'see_file','action' => 'index'));
        }else{
            return $this->redirect(array('controller' => 'see_file','action' => 'ajax',$camera_id));
        }
    }
}